Skip to content

feat(dataframe): add writeCsv with CsvWriteOptions#53

Open
LantaoJin wants to merge 1 commit into
apache:mainfrom
LantaoJin:feat/dataframe-write-csv
Open

feat(dataframe): add writeCsv with CsvWriteOptions#53
LantaoJin wants to merge 1 commit into
apache:mainfrom
LantaoJin:feat/dataframe-write-csv

Conversation

@LantaoJin
Copy link
Copy Markdown
Contributor

@LantaoJin LantaoJin commented May 15, 2026

Which issue does this PR close?

Rationale for this change

DataFrame.writeParquet (#27) lets Java callers materialize a query result, but CSV remains read-only. DataFusion supports DataFrame::write_csv upstream with the full writer-side option surface (delimiter, quote, escape, null token, compression, partitioning, single-file vs directory output). Issue #38 tracks exposing it on the Java side.

The CSV write surface is wider than parquet's — six writer-side knobs plus singleFileOutput and partitionCols — so this PR uses the proto-over-JNI pattern (introduced in #29 and reused by all the read-side option classes) instead of the wide-JNI pattern writeParquet shipped with. Sending a single byte[] keeps the JNI signature stable as more knobs are added.

What changes are included in this PR?

  • proto/csv_write_options.proto — new CsvWriteOptionsProto message. Fields are optional so unset values preserve DataFusion's defaults; partition_cols is repeated so the empty list round-trips unambiguously. FileCompressionType is reused from csv_read_options.proto because the codec set is identical between read and write at the upstream level. Promoting the enum to a shared compression.proto in PR feat(json): expose NdJsonReadOptions via registerJson and readJson #47. Once that lands, this PR's import switches one line.
  • CsvWriteOptions Java builder mirroring the upstream CsvOptions writer-side API: singleFileOutput, partitionCols, hasHeader, delimiter, quote, escape, nullValue, fileCompressionType. All defaults are unset (null) so callers only pay for knobs they touch.
  • DataFrame.writeCsv(String) and DataFrame.writeCsv(String, CsvWriteOptions) overloads with up-front null-arg validation. The receiver remains usable after the call, matching writeParquet's "retain after write" semantics.
  • Java_org_apache_datafusion_DataFrame_writeCsvWithOptions JNI handler in native/src/csv.rs (co-located with the read-side handlers since they share the proto-decode plumbing). Decodes the proto, builds DataFrameWriteOptions and an Option<CsvOptions>, then calls DataFrame::write_csv. Option<CsvOptions> is left as None when no writer knob is set so DataFusion's defaults apply.

Out of scope (for follow-ups):

  • Other writer fields exposed by upstream CsvOptions but not in feat: add DataFrame.writeCsv with CsvWriteOptions #38's checklist: terminator, doubleQuote, dateFormat, datetimeFormat, timestampFormat, timestampTzFormat, timeFormat, compressionLevel, truncatedRows. Easy follow-up — same proto, just add fields.
  • compression_level — separate from the compression codec; the upstream CsvOptions exposes with_compression_level but the issue doesn't list it.

Are these changes tested?

Yes, 11 new tests across CsvWriteOptionsTest and DataFrameWriteCsvTest.

Are there any user-facing changes?

Yes, purely additive. New public API:

  • org.apache.datafusion.CsvWriteOptions
  • DataFrame.writeCsv(String)
  • DataFrame.writeCsv(String, CsvWriteOptions)

The new org.apache.datafusion.protobuf.CsvWriteOptionsProto generated class is also exposed via the protobuf-Java output, consistent with how the read-side option protos are exposed. No API removals, no deprecations, no behavior change for existing callers.

Closes apache#38

Mirrors writeParquet's surface for CSV: writeCsv(path) and
writeCsv(path, options). Options class exposes singleFileOutput,
partitionCols, hasHeader, delimiter, quote, escape, nullValue, and
fileCompressionType.

Uses the proto-over-JNI pattern (CsvWriteOptionsProto) rather than
writeParquet's wide-JNI signature because the writer-side option set
is much wider; a single byte[] keeps the JNI signature stable as more
fields are added later. FileCompressionType is reused from
csv_read_options.proto since the codec set is identical between read
and write.

Option<CsvOptions> on the Rust side stays None when no writer-side
knob is set, so DataFusion's runtime defaults are preserved.

Tests: 4 new options round-trip tests plus 7 integration tests
(default round-trip, single-file, custom delimiter, gzip
round-trip, retain-after-write, null path/options rejection).
make test passes; cargo clippy/fmt and spotless are clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add DataFrame.writeCsv with CsvWriteOptions

1 participant